package cn.naughtior.shch.consumer;

import cn.naughtior.shch.utils.CustomRabbitTemplate;
import cn.naughtior.shch.utils.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author shitou<shitou@in66.com>
 * @create 2017-01-13 14:57
 */

public class Consumer {
    CustomRabbitTemplate amqpTemplate;

    public Object getMessage(String queueName) {
        return amqpTemplate.receiveAndConvert(queueName);
    }

    public List<Object> getMessage(int limit, String queueName) {
        List<Object> list = new ArrayList<Object>();
        while (list.size() < limit) {
            Object object = amqpTemplate.receiveAndConvert(queueName);
            if (object == null) {
                break;
            }
            list.add(object);
        }
        return list;
    }

    public Object getMessage() {
        return amqpTemplate.receiveAndConvert();
    }

    public List<Object> getMessage(int limit) {
        List<Object> list = new ArrayList<Object>();
        while (list.size() < limit) {
            Object object = amqpTemplate.receiveAndConvert();
            if (object == null) {
                break;
            }
            list.add(object);
        }
        return list;
    }

    public List<Message> getMessagesWithNoAutoAck(int limit) {
        List<Message> messages = new ArrayList<Message>();
        while(messages.size() < limit) {
            Message message = ((CustomRabbitTemplate) amqpTemplate).getMessageWithNoAutoAck();
            if(null == message) {
                break;
            }
            messages.add(message);
        }
        return messages;
    }

    public void setAmqpTemplate(CustomRabbitTemplate amqpTemplate) {
        this.amqpTemplate = amqpTemplate;
    }
}
